package com.dhcc.DRGsHosp.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.inject.Inject;

import org.dayatang.utils.Page;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.DRGsHosp.facade.WardFacade;
import com.dhcc.DRGsHosp.facade.dto.WardDTO;

@Controller("drgsHospWebControllerchl2017051102")
@RequestMapping("/Ward")
public class WardController {
		
	@Inject
	private WardFacade wardFacade;
	
	@ResponseBody
	@RequestMapping("/add")
	public InvokeResult add(WardDTO wardDTO) {
		wardFacade.creatWard(wardDTO) ;
		return InvokeResult.success() ;
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public InvokeResult update(WardDTO wardDTO) {
		wardFacade.updateWard(wardDTO) ;
		return InvokeResult.success() ;
	}
	
	@ResponseBody
	@RequestMapping("/pageJson")
	public Page pageJson(WardDTO wardDTO, @RequestParam int page, @RequestParam int pagesize) {
		Page<WardDTO> all = wardFacade.pageQueryWard(wardDTO, page, pagesize);
		return all;
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public InvokeResult remove(@RequestParam String wardCodes) {
		String[] value = wardCodes.split(",");
        String[] wardCodeArrs = new String[value.length];
        for (int i = 0; i < value.length; i ++) {
        	wardCodeArrs[i] =  value[i] ;
		}
        wardFacade.removeWards(wardCodeArrs) ;
        return InvokeResult.success() ;
	}
	
	@ResponseBody
	@RequestMapping("/get/{wardCode}")
	public InvokeResult get(@PathVariable String wardCode) {
		return InvokeResult.success(wardFacade.getWard(wardCode));
	}
		
    @InitBinder    
    public void initBinder(WebDataBinder binder) {  
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");    
        dateFormat.setLenient(false);    
        //CustomDateEditor 可以换成自己定义的编辑器。
        //注册一个Date 类型的绑定器 。
        binder.setAutoGrowCollectionLimit(Integer.MAX_VALUE);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
    
    @ResponseBody
  	@RequestMapping("/findWardByDepCode")
	public InvokeResult findWardByDepCode(String[] depCode){
	  return InvokeResult.success(wardFacade.findWardByDepCode(depCode));
	}
    
    @ResponseBody
    @RequestMapping("/isWardCodeExist")
    public InvokeResult isWardCodeExist(String wardCode) {
    	return InvokeResult.success(wardFacade.isWardCodeExist(wardCode));
    }
    
    /**
     *  @desp  ：监测顺循号是否存在
     *  @Author： lhj
     *  @param wardOrder
     *  @return
     *  @Date  :2018年1月30日上午11:48:56
     */
    @ResponseBody
    @RequestMapping("/isWardOrderExist")
    public boolean isWardOrderExist(String wardOrder,String wardCode) {
    	return wardFacade.isWardOrderExist(wardOrder,wardCode);
    }
    
    @ResponseBody
    @RequestMapping("/getWardNotification")
    public InvokeResult getWardNotification() {
    	return InvokeResult.success(wardFacade.getWardNotification());
    }
    
    @ResponseBody
  	@RequestMapping("/findDepByWardCode")
	public InvokeResult findDepByWardCode(String parDepGroupCode){
	  return InvokeResult.success(wardFacade.findDepByWardCode(parDepGroupCode));
	}
    
    @ResponseBody
  	@RequestMapping("/findDepGroupByDepCode")
	public InvokeResult findDepGroupByDepCode(){
	  return InvokeResult.success(wardFacade.findDepGroupByDepCode());
	}
}
